package lambda;

import java.util.function.IntFunction;

/**
 * 构造方法在Lambda表达式中的调用
 */
public class Lambda04 {
    public static void main(String[] args) {

        //普通lambda表达式
        PersonCreator person = () -> new Person();

        /**构造方法的引用
         * 有参和无参构造器的调用区别在于所定义的接口中构造方法的参数区别
         */
        PersonCreator creator1 = Person::new;
        //无参
        Person person1 = creator1.getPerson();
        System.out.println(person1.toString());;

        //有参
        PersonCreator2 creator2 = Person::new;
        Person person2 = creator2.getPerson("lucy", 12);
        System.out.println(person2.toString());

        //数组的构造方法引用：
        IntFunction<int[]> arrayMaker = int[]::new;//假设有一个返回int类型数组的函数式接口
        int[] array = arrayMaker.apply(5); // 创建数组 int[10]
    }
}
class Person{
    private int age;
    private String name;

    public Person() {
    }

    public Person( String name,int age) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

//需求：返回一个person
interface PersonCreator{
    Person getPerson();
}

interface PersonCreator2{
    Person getPerson(String name, int age);
}
